package cn.itcast.jx.action.sysadmin;

import java.util.List;

import org.springframework.ui.Model;

import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.Module;
import cn.itcast.jx.service.ModuleService;
import cn.itcast.jx.util.Page;

public class ModuleAction extends BaseAction implements ModelDriven<Module>{
	
	private ModuleService moduleService;

	private Module model = new Module();
	private String menuType = "module";

	public String list() {
		String hql = "from Module";
		moduleService.findPage(hql, page, Module.class, null);
		page.setUrl("moduleAction_list");
		push(page);
		return "list";
	}
	
	public String toview() {
		Module module = moduleService.get(Module.class, model.getId());
		push(module);
		
		return "toview";
	}
	
	public String toupdate() {
		Module module = moduleService.get(Module.class, model.getId());
		String hql = "from Module where state = 1";
	
		List<Module> moduleList = moduleService.removeModule( moduleService.find(hql, Module.class, null), module);
		
		put("moduleList", moduleList);
		
		push(module);
		return "toupdate";
	}
	public String update() {
		moduleService.saveOrUpdate(model);
		return "tolist";
	}
	public String tocreate() {
		String hql = "from Module where state = 1";
		List<Module> moduleList = moduleService.find(hql, Module.class, null);
		put("moduleList", moduleList);
			
		return "tocreate";
	}
	public String insert() {
		
		moduleService.saveOrUpdate(model);
		
		return "tolist";
	}
	
	
	public String delete() {
		String[] ids = model.getId().split(", ");
		moduleService.delete(Module.class, ids);
		return "tolist";
	}

	@Override
	public Module getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	private Page page = new Page();
	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
}
